
using Core;
using ET;
using UnityEngine;

public class UnitStateChangeEvent : AEvent<UnitStateChange>
{
    protected override async ETTask Run(UnitStateChange change)
    {
        if(change.newState == UnitSurvivalState.Death)
        {
            var configComponent = World.GetComponent<ConfigComponent>();
            var unitConfig = configComponent.GetConfig<UnitConfig>(change.unit.id);
            if (unitConfig.dropId > 0)
            {
                var loader = configComponent.GetLoader<ItemDropConfigLoader>();
                // 获得掉落道具列表
                var list = loader.GetDrop(unitConfig.dropId);
                if(list.Count > 0)
                {
                    var sceneItemMgr = World.GetComponent<SceneItemMgr>();
                    foreach (var dropCoinfig in list) 
                    {
                        // 创建前验证是否有这个道具
                        var itemConfig = configComponent.GetConfig<ItemConfig>(dropCoinfig.itemId);
                        if (itemConfig != null)
                        {
                            sceneItemMgr.CreateItem(itemConfig.id, dropCoinfig.count, change.unit.Position).Coroutine();
                        }
                    }
                }
            }
        }
    }
}